package com.bao.DaoImpl;

import com.bao.Dao.UserDao;
import com.bao.Emtity.User;
import com.jie.util.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by TangBaoshan on 2018/8/11.
 */
public class UserDaoImpl implements UserDao
{
    @Override
    /**
     * 查询所有的用户信息
     */
    public List<User> findUser() {
        List<User> list = null;
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        String sql = "select * from user where ";
        conn = BaseDao.getConnection();
        try {
            stat = conn.prepareStatement(sql);
            rs = stat.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setUid(rs.getInt("uid"));
                user.setUname(rs.getString("uname"));
                user.setUphone(rs.getString("uphone"));
                user.setUpassword(rs.getString("upassword"));
                user.setUprovince(rs.getInt("uprovince"));
                user.setUcity(rs.getInt("ucity"));
                user.setUcounty(rs.getInt("ucounty"));
                user.setUaddress(rs.getString("uaddress"));
                user.setUsex(rs.getString("usex"));
                user.setUtype(rs.getInt("utype"));
                user.setCreateTime(rs.getString("createTime"));
                user.setOpenid(rs.getString("openid"));
                user.setCheck(rs.getInt("check"));
                user.setUimage(rs.getString("uimage"));
                user.setUemail(rs.getString("uemail"));
                list.add(user);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeConnection(conn, stat, rs);
        }
        return list;
    }

    @Override
    /**
     * 根据姓名查询用户信息
     */
    public List<User> findTypeUser(String name) {
        List<User> lists = new ArrayList<User>();
        String sql = "SELECT * FROM  user WHERE uname=?";
        int num = 0;
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        conn = BaseDao.getConnection();
        try {
            stat = conn.prepareStatement(sql);
            stat.setString(1, name);
            rs = stat.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setUid(rs.getInt("uid"));
                user.setUname(rs.getString("uname"));
                user.setUphone(rs.getString("uphone"));
                user.setUpassword(rs.getString("upasswrod"));
                user.setUprovince(rs.getInt("uprovince"));
                user.setUcity(rs.getInt("ucity"));
                user.setUcounty(rs.getInt("ucounty"));
                user.setUaddress(rs.getString("uaddress"));
                user.setUsex(rs.getString("usex"));
                user.setUtype(rs.getInt("utype"));
                user.setCreateTime(rs.getString("createTime"));
                user.setOpenid(rs.getString("openid"));
                user.setCheck(rs.getInt("check"));
                user.setUimage(rs.getString("uimage"));
                user.setUemail(rs.getString("uemail"));
                lists.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeConnection(conn, stat, rs);
        }
        return lists;
    }

    @Override
    /*
     *新增用户信息
     * */
    public int AddUser(User u) {
        String sql = "INSERT INTO `user`  VALUES (null,?,?,?,?,?,?,?,?,?,NOW(),?,?,?,?);";
        Object[] ob = {
                u.getUname(), u.getUphone(), u.getUpassword(), u.getUprovince(), u.getUcity(), u.getUcounty(), u.getUaddress(), u.getUsex(), u.getUtype(), u.getOpenid(), u.getCheck(), u.getUimage(), u.getUemail()
        };
        return  BaseDao.executeUpdate(sql,ob);
    }

    @Override
    /*
    * 根据用户姓名查询出id
    * */
    public int Findid(String name) {
        String sql = "SELECT uid FROM `user` WHERE uname=?";
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        int num = 0;
        conn = BaseDao.getConnection();
        try {
            stat = conn.prepareStatement(sql);
            stat.setString(1,name);
            rs = stat.executeQuery();
            if (rs.next()) {
                num = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeConnection(conn, stat, rs);
        }
        return num;
    }

    @Override
    public int FindCountUser(String openid) {
        String sql = "SELECT COUNT(1) FROM `user` WHERE openid=?";
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        int num = 0;
        conn = BaseDao.getConnection();
        try {
            stat = conn.prepareStatement(sql);
            stat.setString(1,openid);
            rs = stat.executeQuery();
            if (rs.next()) {
                num = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeConnection(conn, stat, rs);
        }
        return num;
    }

    @Override
    public int FindCountUName(String name) {
        String sql = "SELECT COUNT(1) FROM `user` WHERE uname=?";
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        int num = 0;
        conn = BaseDao.getConnection();
        try {
            stat = conn.prepareStatement(sql);
            stat.setString(1,name);
            rs = stat.executeQuery();
            if (rs.next()) {
                num = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeConnection(conn, stat, rs);
        }
        return num;
    }
}
